package com.goodmean.common;

import com.goodmean.common.Directive.DateDirective;
import com.goodmean.common.interceptor.EncodingInterceptor;
import com.goodmean.common.model.CourseStudentGrade;
import com.goodmean.common.model._MappingKit;
import com.goodmean.common.routes.ProjectRoutes;
import com.goodmean.login.LoginController;

import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.config.Interceptors;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

public class SIMSConfig extends JFinalConfig {
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 80, "/");
	}

	@Override
	public void configConstant(Constants constants) {
		PropKit.use("SIMS_config.properties");
		constants.setDevMode(PropKit.getBoolean("devMode", false));
	}

	@Override
	public void configRoute(Routes routes) {
		//登录模块路由
		routes.add("/", LoginController.class, "/WEB-INF/pages/login");
		//项目路由拦截器
		routes.add(new ProjectRoutes());
	}

	@Override
	public void configEngine(Engine engine) {
		//为项目模板增加时间,模板中调用 #nowDate()即可
		engine.addDirective("nowDate" , DateDirective.class);
		engine.addSharedMethod(new SharedMethod());
		//为整个项目定义模板文件，其他页面可以直接引用，详细可见instructor.html的模板套用
		//engine.addSharedFunction("/common/layout.html");自己建立自己模块内模板
	}

	@Override
	public void configPlugin(Plugins plugins) {
		DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
		plugins.add(druidPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		// 所有映射在 MappingKit 中自动化搞定
		arp.addMapping("course_student_grade" , "course_id,student_id" , CourseStudentGrade.class);
		_MappingKit.mapping(arp);
		plugins.add(arp);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new EncodingInterceptor());
	}

	@Override
	public void configHandler(Handlers handlers) {

	}

	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}

}
